# -*-coding:utf-8-*-

from tornado.gen import coroutine, Return

from delegates.base_impl import BaseDelegateImpl


class BaseDelegate(BaseDelegateImpl):
    @coroutine
    def _update_user_specifics(self, uid, update):
        result = yield super(BaseDelegate, self)._update_user_specifics(uid, update)
        raise Return(result)

    @coroutine
    def _update_device_specifics(self, did, update):
        result = yield super(BaseDelegate, self)._update_device_specifics(did, update)
        raise Return(result)

    @coroutine
    def _update_order_specifics(self, oid, update):
        result = yield super(BaseDelegate, self)._update_order_specifics(oid, update)
        raise Return(result)

    @coroutine
    def _update_product_specifics(self, pid, update):
        result = yield super(BaseDelegate, self)._update_product_specifics(pid, update)
        raise Return(result)

    @coroutine
    def _get_user_specifics(self, **kwargs):
        result = yield super(BaseDelegate, self)._get_user_specifics(**kwargs)
        raise Return(result)

    @coroutine
    def _get_user_role_names(self, **kwargs):
        result = yield super(BaseDelegate, self)._get_user_role_names(**kwargs)
        raise Return(result)

    @coroutine
    def _get_user_common_info(self, **kwargs):
        result = yield super(BaseDelegate, self)._get_user_common_info(**kwargs)
        raise Return(result)

    @coroutine
    def _get_device_specifics(self, did):
        result = yield super(BaseDelegate, self)._get_device_specifics(did)
        raise Return(result)

    @coroutine
    def _get_order_specifics(self, oid):
        result = yield super(BaseDelegate, self)._get_order_specifics(oid)
        raise Return(result)

    @coroutine
    def _get_product_specifics(self, pid):
        result = yield super(BaseDelegate, self)._get_product_specifics(pid)
        raise Return(result)

    def _publish(self, topic, payload):
        super(BaseDelegate, self)._publish(topic, payload)

    def _respond_error(self, error_code, msg=None):
        super(BaseDelegate, self)._respond_error(error_code, msg)

    def _extract_fields(self, src, required_fields=None, optional_fields=None):
        return super(BaseDelegate, self)._extract_fields(src, required_fields, optional_fields)

    def _exclude_fields(self, src, exclusive_fields):
        super(BaseDelegate, self)._exclude_fields(src, exclusive_fields)

    def on_pre_create(self, **kwargs):
        """
        返回 specifics 字段
        :param kwargs: 请求参数
        :return: specifics 字段
        """
        return None

    def on_pre_get(self, **kwargs):
        """
        根据 kwargs 返回查询条件
        :param kwargs: 查询请求参数
        :return:　filter_cond, projection
        """
        return {}, {}

    def on_post_get(self, **kwargs):
        return None

    def on_pre_get_list(self, **kwargs):
        """
        根据 kwargs 返回查询条件
        :param kwargs: 查询请求参数
        :return:　filter_cond, projection
        """
        return {}, {}

    def on_per_list_item(self, item, **kwargs):
        return True

    def on_pre_traverse(self, **kwargs):
        """
        根据 kwargs 返回查询条件
        :param kwargs: 查询请求参数
        :return:　filter_cond, projection
        """
        return {}, {}

    def on_traversing_item(self, item, **kwargs):
        pass

    def on_post_traverse(self, **kwargs):
        """
        在这里整理初步的查询结果
        :param kwargs: 遍历参数，上下文
        :return: 返回最终的查询结果
        """
        pass

    def on_pre_get_statistics(self, **kwargs):
        aggregate_pipeline = []
        default_value = {}
        return aggregate_pipeline, default_value

    def on_pre_update(self, **kwargs):
        return None

    def on_post_update(self, **kwargs):
        return None
